<?php
include 'autoload.php';

// @todo refactor !!
$supportedTags = array( 'Genre', 'Runtime', 'duration', 'Actors', 'Director',
    'Directors', 'Overview', 'Subtitle', 'Produced', 'Rating', 'ActorRoles',
    'Review', 'VolumeName', 'Watched', 'WatchedOn', 'Title' );

if ( !isset( $_GET[ 'id'] ) ) exit('Missing parameter \'id\'' );
$id = $_GET['id'];

if ( !isset( $_GET['library'] ) ) exit( "Missing parameter 'library'" );
$library = $_GET['library'];

$lib = new weebUILibrary( $library );

// Misc init
extract( $tags = $lib->tags() );

if ( !isset($_GET[ 'allocine-id' ] ) )
{

    // custom search query
    if ( isset( $_GET['query'] ) )
    {
        $query = $_GET['query'];
    }
    // search given movie's ID title
    else
    {
        $movieData = sqlite_fetch_array( $lib->query( "SELECT * FROM items WHERE item_id = '$id'" ) );
        $title = $movieData['item_name'];
        $title = preg_replace( '/(\([0-9]+\))/', '', $title );
        $title = preg_replace( '/\[[^\]]+\]/', '', $title );
        $query = $title;
    }

    $search = new weebUIAllocineSearch();
    $search->query = $query;
    try {
        $search->run();

        if ( $search->perfectMatch )
        {
            $movie = $search->perfectMatch;
            $movie->load();
        }
        else
        {
            echo "<table border=\"0\" cellpadding=\"3\" cellspacing)\"0\">";
            foreach ( $search->results as $result )
            {
                $url = "allocine.php?library=$library&id=$id&allocine-id={$result->id}";
                echo "<tr>\n";
                echo "<td><a href=\"$url\"><img src=\"{$result->cover}\" style=\"border-width: 0px; width: 60px; height: 80px; \"></td>\n";
                echo "<td>{$result->title} ({$result->year})<br />de {$result->by}</a></td>";
                echo "</tr>\n";
            }
            echo "</table>";
        }
    } catch( weebUIAllocineSearchNoResultsException $e )
    {
        echo "<p>No results found while looking for '$title'</p>";
        echo "<form method=\"get\" action=\"allocine.php\">\n";
        echo "<input type=\"hidden\" name=\"library\" value=\"$library\" />\n";
        echo "<input type=\"hidden\" name=\"id\" value=\"$id\" />\n";
        echo "Custom search query <input type=\"text\" name=\"query\" value=\"$query\" />\n";
        echo "<input type=\"submit\" value=\"Search again\" />\n";
        echo "</form>\n";
    }
}
else
{
    $allocineID = $_GET['allocine-id'];
    $movie = new weebUIAllocineItem();
    $movie->id = $allocineID;
}

if ( $movie )
{
    $movie->load();

    $extension = substr( $movie->cover, -3 );
    $targetName = preg_replace( '/[^a-z0-9]/', '', strtolower( $movie->title ) . '_' . $movie->releaseDate );
    $filePath = 'C:\\Meedio\\MeedioData\\Cache\\Nicecovers Stills\\' . $targetName . '.' . $extension;
    copy( $movie->cover, $filePath );

    $fields[] = "$tag_Title = '" . sqlite_escape_string( $movie->title ) . "'";
    $fields[] = "$tag_Overview = '" . sqlite_escape_string( $movie->synopsis ) . "'";
    $fields[] = "$tag_Runtime = '" . sqlite_escape_string( $movie->runtime ) . "'";
    $fields[] = "$tag_Produced = '" . sqlite_escape_string( $movie->releaseDate ) . "'";
    $fields[] = "$tag_Director = '" . sqlite_escape_string( $movie->directors ) . "'";
    $fields[] = "$tag_Actors = '" . sqlite_escape_string( $movie->actors ) . "'";
    $fields[] = "$tag_ActorRoles = '" . sqlite_escape_string( $movie->actorsRoles ) . "'";
    $fields[] = "$tag_Genre = '|" . sqlite_escape_string( implode( '|', $movie->genres ) ) . "|'";
    $fields[] = "item_image = '" . sqlite_escape_string( $filePath ) . "'";

    $query = "UPDATE items SET " . implode( ', ', $fields ) . " WHERE item_id = $id";
    $lib->updateQuery( $query );

    header( "Location: index.php?section=library&library=$library&id=$id" );
}

?>